package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.SysTypeConfig;
import com.ruoyi.recycle.domain.TrainVideo;
import com.ruoyi.recycle.dto.TrainVideoDTO;
import com.ruoyi.recycle.service.ITrainVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 培训视频Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/video")
@Api(tags = "培训视频")
public class TrainVideoController extends BaseController {
    @Autowired
    private ITrainVideoService trainVideoService;

    /**
     * 查询培训视频列表
     */
    @RequiresPermissions("recycle:video:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainVideo trainVideo) {
        startPage();
        List<TrainVideo> list = trainVideoService.selectTrainVideoList(trainVideo);
        return getDataTable(list);
    }

    /**
     * 分页查询培训视频列表
     */
    @RequiresPermissions("recycle:video:list")
    @PostMapping("/videoPageQuery")
    @ApiOperation("分页查询培训视频列表")
    public IPage<TrainVideo> videoPageQuery(@RequestBody TrainVideoDTO queryDTO) throws Exception{
        final TrainVideo factory = new TrainVideo();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<TrainVideo> wrapper = Wrappers.lambdaQuery(factory);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(TrainVideo::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(TrainVideo::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(TrainVideo::getCreatedTime);
        return trainVideoService.page(new Page<TrainVideo>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出培训视频列表
     */
    @RequiresPermissions("recycle:video:export")
    @Log(title = "培训视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出培训视频列表")
    public void export(HttpServletResponse response, TrainVideo trainVideo) {
        List<TrainVideo> list = trainVideoService.selectTrainVideoList(trainVideo);
        ExcelUtil<TrainVideo> util = new ExcelUtil<TrainVideo>(TrainVideo.class);
        util.exportExcel(response, list, "培训视频数据");
    }

    /**
     * 获取培训视频详细信息
     */
    @RequiresPermissions("recycle:video:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取培训视频详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(trainVideoService.selectTrainVideoById(id));
    }

    /**
     * 新增培训视频
     */
    @RequiresPermissions("recycle:video:add")
    @Log(title = "培训视频", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增培训视频")
    public AjaxResult add(@RequestBody TrainVideo trainVideo) {
        trainVideo.setCreatedTime(new Date());
        return toAjax(trainVideoService.insertTrainVideo(trainVideo));
    }

    /**
     * 修改培训视频
     */
    @RequiresPermissions("recycle:video:edit")
    @Log(title = "培训视频", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改培训视频")
    public AjaxResult edit(@RequestBody TrainVideo trainVideo) {
        trainVideo.setUpdatedTime(new Date());
        return toAjax(trainVideoService.updateTrainVideo(trainVideo));
    }

    /**
     * 删除培训视频
     */
    @RequiresPermissions("recycle:video:remove")
    @Log(title = "培训视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除培训视频")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(trainVideoService.deleteTrainVideoByIds(ids));
    }
}
